﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Text;
using System.IO;

using SenerPro;


namespace MensUnoShop.Models
{
    public class KeyWordFactory
    {
        private static Logger _log = new Logger(Config.logpath);

        public static XElement GetHotKeyList()
        {
            XElement xe;
            using (StreamReader sr = new StreamReader(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]))
            {
                xe = XElement.Load(sr);
            }
            return xe;
        }

        public static List<UHotKey> GetManagerHotKeyList()
        {
            DBA.DBKeyword dbk = new DBA.DBKeyword();
            return dbk.GetManagerHotKeyList();
        }

        public static bool AddKeyword(string keyword, ref string msg)
        {
            XElement xe;
            bool isadd = false;
            using (StreamReader sr = new StreamReader(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]))
            {
                xe = XElement.Load(sr);
            }
            try{
                if (xe.Elements("HotKey").Any(h => h.Element("Keyword").Value == keyword))
                    msg = "關鍵字已在列表中!";
                else
                {
                    xe.AddFirst(new XElement("HotKey", new XAttribute("Color", "0"),
                                    new XElement("Keyword", keyword)));
                    xe.Save(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]);
                    isadd = true;
                }
            }
            catch(Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddKeyword", ex.ToString());
            }
            return isadd;
        }

        public static bool DelKeyword(string keyword, ref string msg)
        {
            XElement xe;
            bool isadd = false;
            using (StreamReader sr = new StreamReader(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]))
            {
                xe = XElement.Load(sr);
            }
            try
            {
                if (!xe.Elements("HotKey").Any(h => h.Element("Keyword").Value == keyword))
                    msg = "關鍵字不存在!";
                else
                {
                    xe.Elements("HotKey").FirstOrDefault(h=>h.Element("Keyword").Value==keyword).Remove();
                    xe.Save(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]);
                    isadd = true;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelKeyword", ex.ToString());
            }
            return isadd;
        }

        public static bool SetKeywordColor(string keyword, int cid, ref string msg)
        {
            XElement xe;
            bool isadd = false;
            using (StreamReader sr = new StreamReader(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]))
            {
                xe = XElement.Load(sr);
            }
            try
            {
                if (!xe.Elements("HotKey").Any(h => h.Element("Keyword").Value == keyword))
                    msg = "關鍵字不存在!";
                else
                {
                    xe.Elements("HotKey").FirstOrDefault(h => h.Element("Keyword").Value == keyword).Attribute("Color").SetValue(cid.ToString());
                    xe.Save(System.Configuration.ConfigurationManager.AppSettings["HotKeyConfig"]);
                    isadd = true;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetKeywordColor", ex.ToString());
                msg = ex.ToString();
            }
            return isadd;
        }
    }
}